package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.TypesOfInsuranceService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.TypesOfInsuranceDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing TypesOfInsurance.
 */
@RestController
@RequestMapping("/api")
public class TypesOfInsuranceResource {

    private final Logger log = LoggerFactory.getLogger(TypesOfInsuranceResource.class);

    private static final String ENTITY_NAME = "typesOfInsurance";

    private final TypesOfInsuranceService typesOfInsuranceService;

    public TypesOfInsuranceResource(TypesOfInsuranceService typesOfInsuranceService) {
        this.typesOfInsuranceService = typesOfInsuranceService;
    }

    /**
     * POST  /types-of-insurances : Create a new typesOfInsurance.
     *
     * @param typesOfInsuranceDTO the typesOfInsuranceDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new typesOfInsuranceDTO, or with status 400 (Bad Request) if the typesOfInsurance has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/types-of-insurances")
    @Timed
    public ResponseEntity<TypesOfInsuranceDTO> createTypesOfInsurance(@RequestBody TypesOfInsuranceDTO typesOfInsuranceDTO) throws URISyntaxException {
        log.debug("REST request to save TypesOfInsurance : {}", typesOfInsuranceDTO);
        if (typesOfInsuranceDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new typesOfInsurance cannot already have an ID")).body(null);
        }
        TypesOfInsuranceDTO result = typesOfInsuranceService.save(typesOfInsuranceDTO);
        return ResponseEntity.created(new URI("/api/types-of-insurances/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /types-of-insurances : Updates an existing typesOfInsurance.
     *
     * @param typesOfInsuranceDTO the typesOfInsuranceDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated typesOfInsuranceDTO,
     * or with status 400 (Bad Request) if the typesOfInsuranceDTO is not valid,
     * or with status 500 (Internal Server Error) if the typesOfInsuranceDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/types-of-insurances")
    @Timed
    public ResponseEntity<TypesOfInsuranceDTO> updateTypesOfInsurance(@RequestBody TypesOfInsuranceDTO typesOfInsuranceDTO) throws URISyntaxException {
        log.debug("REST request to update TypesOfInsurance : {}", typesOfInsuranceDTO);
        if (typesOfInsuranceDTO.getId() == null) {
            return createTypesOfInsurance(typesOfInsuranceDTO);
        }
        TypesOfInsuranceDTO result = typesOfInsuranceService.save(typesOfInsuranceDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, typesOfInsuranceDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /types-of-insurances : get all the typesOfInsurances.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of typesOfInsurances in body
     */
    @GetMapping("/types-of-insurances")
    @Timed
    public ResponseEntity<List<TypesOfInsuranceDTO>> getAllTypesOfInsurances(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of TypesOfInsurances");
        Page<TypesOfInsuranceDTO> page = typesOfInsuranceService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/types-of-insurances");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /types-of-insurances/:id : get the "id" typesOfInsurance.
     *
     * @param id the id of the typesOfInsuranceDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the typesOfInsuranceDTO, or with status 404 (Not Found)
     */
    @GetMapping("/types-of-insurances/{id}")
    @Timed
    public ResponseEntity<TypesOfInsuranceDTO> getTypesOfInsurance(@PathVariable Long id) {
        log.debug("REST request to get TypesOfInsurance : {}", id);
        TypesOfInsuranceDTO typesOfInsuranceDTO = typesOfInsuranceService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(typesOfInsuranceDTO));
    }

    /**
     * DELETE  /types-of-insurances/:id : delete the "id" typesOfInsurance.
     *
     * @param id the id of the typesOfInsuranceDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/types-of-insurances/{id}")
    @Timed
    public ResponseEntity<Void> deleteTypesOfInsurance(@PathVariable Long id) {
        log.debug("REST request to delete TypesOfInsurance : {}", id);
        typesOfInsuranceService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/types-of-insurances?query=:query : search for the typesOfInsurance corresponding
     * to the query.
     *
     * @param query the query of the typesOfInsurance search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/types-of-insurances")
    @Timed
    public ResponseEntity<List<TypesOfInsuranceDTO>> searchTypesOfInsurances(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of TypesOfInsurances for query {}", query);
        Page<TypesOfInsuranceDTO> page = typesOfInsuranceService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/types-of-insurances");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
